package com.whfc.uni.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.uni.dao.CarWashDeviceDao;
import com.whfc.uni.entity.CarWashDevice;
import com.whfc.uni.service.CarWashDeviceService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService(interfaceClass = CarWashDeviceService.class, version = "1.0.0")
public class CarWashDeviceServiceImpl implements CarWashDeviceService {

    @Autowired
    private CarWashDeviceDao carWashDeviceDao;

    @Override
    public void add(CarWashDevice carWashDevice) throws BizException {
        CarWashDevice one = carWashDeviceDao.selectBySnAndPlatformAndType(carWashDevice.getSn(),carWashDevice.getPlatform(),carWashDevice.getDeviceType());
        if (one != null ) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "硬件SN已经被绑定");
        }
        carWashDeviceDao.insertSelective(carWashDevice);
    }

    @Override
    public void edit(CarWashDevice carWashDevice) throws BizException {
        if (StringUtils.hasText(carWashDevice.getSn())) {
            CarWashDevice one = carWashDeviceDao.selectBySnAndPlatformAndType(carWashDevice.getSn(),carWashDevice.getPlatform(),carWashDevice.getDeviceType());
            if (one != null && one.getId()!=carWashDevice.getId() ) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "硬件SN已经被绑定");
            }
        }
        carWashDeviceDao.updateByPrimaryKeySelective(carWashDevice);
    }

    @Override
    public PageData<CarWashDevice> listPage(Integer pageNum, Integer pageSize, Integer deptId, String name, Integer type) {
        PageHelper.startPage(pageNum, pageSize);
        List<CarWashDevice> list = carWashDeviceDao.selectList(deptId,name,type);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public List<CarWashDevice> list(Integer deptId, String name, Integer type) {
        return carWashDeviceDao.selectList(deptId,name,type);
    }

    @Override
    public void del(Integer id) throws BizException {
        carWashDeviceDao.deleteByPrimaryKey(id);
    }

    @Override
    public CarWashDevice selectBySnAndPlatformAndType(String sn, String platform, Integer deviceType) {
        return carWashDeviceDao.selectBySnAndPlatformAndType(sn,platform,deviceType);
    }
}
